/**
 * Copyright (c) 2018 qar All rights reserved.
 *
 * https://www.qar.io
 *
 * 版权所有，侵权必究！
 */

package com.example.mybatis.handler;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.example.mybatis.enums.DelFlagEnum;
import com.example.security.SecurityUser;
import com.example.security.UserDetail;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * 公共字段，自动填充值
 *
 * @author Mark sunlightcs@gmail.com
 */
@Component
public class FieldMetaObjectHandler implements MetaObjectHandler {
    @Override
    public void insertFill(MetaObject metaObject) {
        strictInsertFill(metaObject, "creator", Long.class, SecurityUser.getUserId());
        strictInsertFill(metaObject, "createDate", Date.class, new Date());
        strictInsertFill(metaObject, "tenantId", Long.class, SecurityUser.getTenantId());
        strictInsertFill(metaObject, "storeId", Long.class, SecurityUser.getStoreId());
        strictInsertFill(metaObject, "delFlag", Integer.class, 0);
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        strictUpdateFill(metaObject, "updator", Long.class, SecurityUser.getUserId());
        strictUpdateFill(metaObject, "updateDate", Date.class, new Date());
        strictUpdateFill(metaObject, "tenantId", Long.class, SecurityUser.getTenantId());
        strictUpdateFill(metaObject, "storeId", Long.class, SecurityUser.getStoreId());
    }
}